/* eslint-disable eqeqeq */
const { readJSON, writeFile, buildId } = require('../utils/index')
const SHA1 = require('crypto-js/sha1')
const hexMd5 = require('../utils/hex_md5')
const ENCODE_TYPE = 'hexMd5'
const ENCODE = {
  SHA1,
  hexMd5
}
const FileName = 'subject.json'
let TEMP_LIST = null
const get = () => {
  const t = readJSON(FileName)
  if (!TEMP_LIST) {
    TEMP_LIST = t
  }
  const list = TEMP_LIST.list.map((it, idx) => {
    console.log(it)
    return {
      id: it.id,
      key: idx,
      subjectName: it.subjectName,
      type: it.type,
      tagName: it.tagName,
      explain: it.explain,
      kps: it.kps || '',
      options: it.options.map((i) => i.content),
      ans: it.options.filter((i) => i.checked).map((it) => {
        // const en = ENCODE[ENCODE_TYPE](it.content).words.join('')
        const en = ENCODE[ENCODE_TYPE](it.content)
        return en
      })
    }
  })
  list.reverse()

  return { list }
}

/**
 * 更加
 * @param {*} id
 * @returns
 */
const getId = (id) => {
  // console.log('---------------', id)
  if (!TEMP_LIST) {
    const t = readJSON(FileName)
    TEMP_LIST = t
  }
  // console.dir(TEMP_LIST.list)
  const data = TEMP_LIST.list.find((it) => it.id == id)
  // console.log('查找结果...')
  // console.dir(data)
  return {
    id: data.id,
    keyword: data.keyword,
    subjectName: data.subjectName,
    type: data.type,
    tagName: data.tagName || '',
    options: data.options.map((i) => i.content),
    explain: data.explain || '',
    kps: data.kps || '',
    ans: data.options
      .filter((i) => i.checked)
      .map((it) => ENCODE[ENCODE_TYPE](it.content))
  }
}

const add = ({ options, subjectName, type, explain, tagName, kps }) => {
  const id = buildId()
  if (!TEMP_LIST) {
    TEMP_LIST = readJSON(FileName)
  }
  TEMP_LIST.list.push({ id, options, subjectName, type, explain, tagName, kps })
  TEMP_LIST.lastM = Date.now()

  writeFile(FileName, JSON.stringify(TEMP_LIST))
  return { id, type, options, subjectName, tagName, explain, kps }
}

const put = ({ id, options, subjectName, type, explain, tagName }) => {
  if (!TEMP_LIST) {
    TEMP_LIST = readJSON(FileName)
  }
  const idx = TEMP_LIST.list.findIndex((it) => it.id === id)
  TEMP_LIST.list[idx] = { id, options, subjectName, type, tagName, explain }
  TEMP_LIST.lastM = Date.now()

  console.log('修改.......', id, idx)
  writeFile(FileName, JSON.stringify(TEMP_LIST))
  return { id, type, options, subjectName, tagName }
}

const patch = ({ id, ...data }) => {
  if (!TEMP_LIST) {
    TEMP_LIST = readJSON(FileName)
  }
  const idx = TEMP_LIST.list.findIndex((it) => it.id === id)
  if (idx < 0) {
    return 'error'
  }
  TEMP_LIST.list[idx] = { ...TEMP_LIST.list[idx], ...data }
  TEMP_LIST.lastM = Date.now()

  console.log('修改.......', id, idx)
  writeFile(FileName, JSON.stringify(TEMP_LIST))
  return TEMP_LIST.list[idx]
}

const del = (id) => {
  if (!TEMP_LIST) {
    TEMP_LIST = readJSON(FileName)
  }
  const idx = TEMP_LIST.list.findIndex((it) => it.id === id)
  console.log('删除......', id, idx)
  if (idx > -1) {
    TEMP_LIST.list.splice(idx, 1)
    TEMP_LIST.lastM = Date.now()

    writeFile(FileName, JSON.stringify(TEMP_LIST))
    return { id, success: true }
  } else {
    return { success: false }
  }
}

module.exports = {
  get,
  getId,
  add,
  del,
  put,
  patch
}
